'''
Created on May 13, 2010

@author: oabalbin
'''
import sys
import os
import numpy as np
import scipy as sp
from scipy import stats
from collections import deque

#from numpy.random import permutation


import signatures.parsers.read_gene_lists as gpm
import signatures.stats.Rstats as rstat
import signatures.preprocess.tools as tl
from signatures.common.classes import arrayMatrix, cpmMatrix 
from signatures.stats.Rstats import calc_matrix_correlation, calc_hypergeometric_test


def corr_permutation_distr(nlabels, nperm, set_size):
    """
    It calculates a permutation distribution for the values of the correlation
    """
    matrix_permutations=np.zeros( (nperm, set_size),dtype=np.int )
    
    for i in range(nperm):    
        matrix_permutations[i,:] = np.random.random_integers(0,nlabels, set_size)
            
    return matrix_permutations

def calc_correlation_of_permutation(corrtype, matrix1, matrix2, twoMatrices):
    """
    """
    correlation_matrix = calc_matrix_correlation(corrtype, matrix1, twoMatrices, matrix2)
    return correlation_matrix

def compute_permutations_test(datamatrix1, datamatrix2, original_correlation_mat, npermutations, numberOfFeatures2Sample, totalNumFeatures, chooseData2Permuted, corrtype, twoMatrices=True):
    """
    """
    percentile = 90
    # Calculate statistics for the original correlation matrix
    orig_mat_statistic = statistics_on_corrMatrix_percentile(original_correlation_mat, percentile)
    
    # start the permutation test
    
    matrix_permutations = corr_permutation_distr(totalNumFeatures, npermutations, numberOfFeatures2Sample)
    permutation_statistics = np.empty( (npermutations) )
    
    for i in range(npermutations):
        thisper = matrix_permutations[i,:]
        print thisper 
        if chooseData2Permuted == "genes":
            correlation_matrix = calc_correlation_of_permutation(corrtype, datamatrix1[thisper,:], datamatrix2, twoMatrices) 
            
        elif chooseData2Permuted == "TUs":
            correlation_matrix = calc_correlation_of_permutation(corrtype, datamatrix1, datamatrix2[thisper,:], twoMatrices)
         
        # execute function       
        permutation_statistics[i] = statistics_on_corrMatrix_percentile(correlation_matrix, percentile)
        
    # Calculate pvalue. 
    
    permutations_pvalue = len(permutation_statistics[permutation_statistics > orig_mat_statistic])/float(npermutations)
    
    return permutations_pvalue

def statistics_on_corrMatrix_percentile(correlation_matrix, percentile):
    """
    It calculates the 75 percentile statistics in the correlation matrix
    It returns the average of matrix_percentiles.
    The rational is that this averag00e should be very different between the original data set and 
    a random data set. 
    """
    
    matrix_percentiles = np.zeros((correlation_matrix.shape[1]))
    for j in range(correlation_matrix.shape[1]):
        matrix_percentiles[j] = sp.stats.scoreatpercentile(correlation_matrix[:,j],percentile)
    
    return np.mean(matrix_percentiles)



corrtype = 'Sper'
datamatrix1=np.random.random_integers(-1,10,(6,6))
datamatrix2=np.random.random_integers(-1,10,(6,6))
npermutations=100
numberOfFeatures2Sample=5
totalNumFeatures=5
chooseData2Permuted="genes"

original_correlation_mat=calc_correlation_of_permutation(corrtype,datamatrix1,datamatrix2,True)

permutations_pvalue = compute_permutations_test(datamatrix1, datamatrix2, original_correlation_mat, npermutations, numberOfFeatures2Sample, totalNumFeatures, chooseData2Permuted, corrtype, twoMatrices=True)    

print permutations_pvalue

